var regphone = /^1[3|4|5|7|8]\d{9}$/;
var regnum = /^([1-9][0-9]*|0)$/;


remove_advertising();

/**
 * 获取验证码
 */
function verify() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var phone = $("#phone").val();
    if (isempty(phone)) {
        $("#hintbg").show();
        $("#hint_message").html("请输入手机号");
        return;
    } else if (!regphone.test(phone)) {
        $("#hintbg").show();
        $("#hint_message").html("请输入正确格式的手机号");
        return;
    } else {
        var url = "/except/usercontroller/getphonecode.action";
        var jsonstr = "{token:" + token + "}";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                "json": jsonstr,
                type: 0,
                tag: "hq",
                phone:phone
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 20000) {
                    $("#hintbg").show();
                    $("#hint_message").html("获取成功，请在五分钟内填写");
                    $("#sec").val(60);
                } else if (code == 10001 || code == 10002) {
                    location.href = "/except/login.jsp";
                } else {
                    $("#hintbg").show();
                    $("#hint_message").html(message);
                }
            }
        });
    }
}

/**
 * 点击绑定
 */
function binding_phone() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var phone = $("#phone").val();
    if (isempty(phone)) {
        $("#hintbg").show();
        $("#hint_message").html("请输入手机号");
        return;
    } else if (!regphone.test(phone)) {
        $("#hintbg").show();
        $("#hint_message").html("请输入正确格式的手机号");
        return;
    }
    var yzcode = $("#yz_code").val();
    if(isempty(yzcode)) {
        $("#hintbg").show();
        $("#hint_message").html("请输入验证码");
        return;
    } else if(!regnum.test(yzcode)) {
        $("#hintbg").show();
        $("#hint_message").html("请输入正确格式的验证码");
        return;
    }
    var url = "/except/usercontroller/getphonecode.action";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            type: 0,
            tag: "yz",
            phone:phone,
            code:yzcode
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                var phonecode = data.data.phonecode;
                bindingphone(phone, phonecode);
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        }
    });
}


function bindingphone(phone, phonecode) {
    var url = "/except/usercontroller/bindingphone.action";
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            phone:phone,
            phonecode:phonecode
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                $("#hintbg").show();
                $("#hint_message").html("绑定成功");
                location.href = "/except/files/userinfo.jsp";
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        },
        error:function (data) {
            $("#hintbg").show();
            $("#hint_message").html("网络异常");
        }
    });
}

/**
 * 倒计时
 */
setInterval("updateYzcodeSec()", 1000);//1000为1秒钟
function updateYzcodeSec() {
    var sec = $("#sec").val();
    if (isempty(sec))
        return;
    sec = sec * 1 - 1;
    if (sec == 0) {
        $("#sec").val("");
        $("#vrifycode").html('再次获取');
    } else {
        $("#sec").val(sec);
        $("#vrifycode").html(sec + '秒后获取');
    }
}